﻿using BCCServer.DAO.Interfaces;
using BCCServer.Model.Models;
using BCCServer.Model.ModelsOut;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BCCServer.DAO.login
{
    public class loginDB: IloginDB
    {
        //protected static bccContext context = new Model.Models.bccContext();

        private readonly bccContext _context;

        public loginDB(bccContext Context)
        {
            this._context = Context;
        }

        /// <summary>
        /// 根据账号查询聊天信息
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <param name="receiveAccount"></param>
        /// <returns></returns>
        public  BCCUserOut selLogin(string Account, string pwd)
        {
            //var password = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5");
            string pwdMD5 = string.Empty;
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(pwd));
                var strResult = BitConverter.ToString(result);
                pwdMD5 = strResult.Replace("-", "");
            }
            
            var bccUser = _context.UUsers.Where(i => i.Password == pwdMD5 && i.Account == Account).AsEnumerable().Select(item => new BCCUserOut
            {
                Id = item.Id,
                Account = item.Account,
                UserName = item.UserName,
                UserSign = item.UserSign,
                Address = item.Address,
                Telephone = item.Telephone,
                Mail = item.Mail,
                RoleId = item.RoleId
            }).FirstOrDefault();

            return bccUser;
        }
    }
}
